import 'package:flutter/material.dart';

class Flutter_Animation_Page extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return Flutter_Animation_State();
  }
}

class Flutter_Animation_State extends State<Flutter_Animation_Page>
    with SingleTickerProviderStateMixin {

  Animation<double> animation;
  AnimationController controller;

  @override
  void initState() {
    super.initState();
    controller = AnimationController(
        vsync: this, duration: Duration(milliseconds: 2000));
    animation = Tween(begin: 0.0, end: 300.0)
    //animate()传入一个控制器对象,返回的是一个Animation
        .animate(controller)
    //该addListener()函数调用了setState()，所以每次动画生成一个新的数字时，当前帧被标记为脏(dirty)，这会导致widget的build()方法再次被调用
      ..addListener(
              () {
            setState(() {

            });
          }
      )
    //addStatusListener来监听动画状态更改的通知，AnimationStatus.forward开始，AnimationStatus.completed结束
      ..addStatusListener((status) {
        if (status == AnimationStatus.completed) {
          controller.reverse(); //动画倒着执行
        } else if (status == AnimationStatus.dismissed) {
          controller.forward(); //动画开始执行
        }
      });

    controller.forward();
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        color: Colors.white,
        width: animation.value.toDouble(),
        height: animation.value.toDouble(),
        child: FlutterLogo(),
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
//    调用dispose()方法,以防止内存泄漏
    controller.dispose();
  }
}